package com.example.firststory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class Historia extends Activity {

	TextView titulo, texto, precio;
	Bundle extras;
	Intent traspaso;
	Button seguir;
	ImageButton atras;
	Animation animacion;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_historia);

		getReferencias();
		setTexto();
		listenerBotones();
		setTipografias();

	}

	protected void onNewIntent(Intent data) {

		extras = data.getExtras();
		setTexto();
		if (!Datos.getNombre().equalsIgnoreCase("")) {
			listenerBotonInfo();
		}

	}

	protected void getReferencias() {

		titulo = (TextView) findViewById(R.id.tvTituloHist);
		texto = (TextView) findViewById(R.id.tvInfoHist);
		atras = (ImageButton) findViewById(R.id.btnAtrasHist);
		seguir = (Button) findViewById(R.id.btnSeguirHist);
		precio = (TextView) findViewById(R.id.tvPrecioHist);

		extras = getIntent().getExtras();

		animacion = AnimationUtils.loadAnimation(this, R.anim.clic_btn);
	}

	protected void setTexto() {

		switch (extras.getInt("Historia")) {
		case 1:

			titulo.setText(R.string.tituloHistoria1);
			texto.setText(R.string.textoHistoria1);
			break;

		case 2:

			titulo.setText(R.string.tituloHistoria2);
			texto.setText(R.string.textoHistoria2);
			break;

		case 3:

			titulo.setText(R.string.tituloHistoria3);
			texto.setText(R.string.textoHistoria3);
			break;

		}

	}

	protected void listenerBotones() {

		atras.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				setActivity(SeleccionarHistoria.class);
				cambiarActivity();

			}
		});

		seguir.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Datos.setHistoria(String.valueOf(titulo.getText()));
				setActivity(Genero.class);
				cambiarActivity();

			}
		});
		
		seguir.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				animacion.reset();
				seguir.startAnimation(animacion);

				return false;
			}
		});
		
		atras.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				animacion.reset();
				atras.startAnimation(animacion);

				return false;
			}
		});
	}

	protected void listenerBotonInfo() {

		seguir.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Datos.setHistoria(String.valueOf(titulo.getText()));
				setActivity(Informacion.class);
				cambiarActivity();

			}
		});

		seguir.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				animacion.reset();
				seguir.startAnimation(animacion);

				return false;
			}
		});
		
		atras.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				animacion.reset();
				atras.startAnimation(animacion);

				return false;
			}
		});
	}

	@SuppressWarnings("rawtypes")
	protected void setActivity(Class clase) {

		traspaso = new Intent(this, clase);

	}

	protected void cambiarActivity() {

		traspaso.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(traspaso);

	}

	public void onBackPressed() {

	}

	public void setTipografias() {

		Config.setTypefaceRegular(this, texto);
		Config.setTypefaceRegular(this, precio);
		Config.setTypefaceNegrita(this, titulo);
		Config.setTypefaceNegrita(this, seguir);

	}

}
